//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BarrierOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CBarrierOption::CBarrierOption (QL::Instruments::Barrier::TypeEnum barrierType, Double barrier, Double rebate, Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CBarrierOption::typeid)
{
    CStrikedTypePayoff^ _Cpayoff;
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phBarrierOption = NULL;
#endif
        QuantLib::Barrier::Type _barrierType = (QuantLib::Barrier::Type)barrierType ;
        QuantLib::Real _barrier = (QuantLib::Real)ValueHelper::Convert (barrier);
        QuantLib::Real _rebate = (QuantLib::Real)ValueHelper::Convert (rebate);
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (new QuantLib::BarrierOption ( _barrierType,  _barrier,  _rebate,  _payoff,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppBarrierOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppBarrierOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Instruments::CBarrierOption::CBarrierOption (boost::shared_ptr<QuantLib::BarrierOption>& childNative, Object^ owner) : COneAssetOption(CBarrierOption::typeid)
{
#ifdef HANDLE
	_phBarrierOption = NULL;
#endif
	_ppBarrierOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppBarrierOption));
}
Cephei::QL::Instruments::CBarrierOption::CBarrierOption (QuantLib::BarrierOption& childNative, Object^ owner) : COneAssetOption(CBarrierOption::typeid)
{
#ifdef HANDLE
	_phBarrierOption = NULL;
#endif
	_ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppBarrierOption));
    _BarrierOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Instruments::CBarrierOption::CBarrierOption (CBarrierOption^ copy) : COneAssetOption(CBarrierOption::typeid)
{
#ifdef HANDLE
	_phBarrierOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppBarrierOption));
    }
}
Cephei::QL::Instruments::CBarrierOption::CBarrierOption (System::Type^ t) : COneAssetOption(CBarrierOption::typeid)
{
#ifdef HANDLE
	_phBarrierOption = NULL;
#endif
	if (!t->IsSubclassOf(CBarrierOption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CBarrierOption::CBarrierOption (QuantLib::Handle<QuantLib::BarrierOption>& childNative, Object^ owner)  : COneAssetOption(CBarrierOption::typeid)
{
	_phBarrierOption = &childNative;
	_ppBarrierOption = &static_cast<boost::shared_ptr<QuantLib::BarrierOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppBarrierOption));
    _BarrierOptionOwner = owner;
}
Cephei::QL::Instruments::CBarrierOption::CBarrierOption (QuantLib::Handle<QuantLib::BarrierOption> childNative)  : COneAssetOption(CBarrierOption::typeid)
{
	_phBarrierOption = &childNative;
	_ppBarrierOption = &static_cast<boost::shared_ptr<QuantLib::BarrierOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppBarrierOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CBarrierOption::CBarrierOption (QuantLib::BarrierOption childNative)  : COneAssetOption(CBarrierOption::typeid)
{
#ifdef HANDLE
	_phBarrierOption = NULL;
#endif
	_ppBarrierOption = new boost::shared_ptr<QuantLib::BarrierOption> (new QuantLib::BarrierOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppBarrierOption));
}
#endif

Cephei::QL::Instruments::CBarrierOption::~CBarrierOption ()
{
    if (_ppBarrierOption != NULL)
    {
	    delete _ppBarrierOption;
        _ppBarrierOption = NULL;
    }
}
Cephei::QL::Instruments::CBarrierOption::!CBarrierOption ()
{
    if (_ppBarrierOption != NULL)
    {
	    delete _ppBarrierOption;
    }
}
QuantLib::BarrierOption& Cephei::QL::Instruments::CBarrierOption::GetReference ()
{
    if (_ppBarrierOption == NULL) throw gcnew NativeNullException ();
	return **_ppBarrierOption;
}
boost::shared_ptr<QuantLib::BarrierOption>& Cephei::QL::Instruments::CBarrierOption::GetShared ()
{
    if (_ppBarrierOption == NULL) throw gcnew NativeNullException ();
	return *_ppBarrierOption;
}
QuantLib::BarrierOption* Cephei::QL::Instruments::CBarrierOption::GetPointer ()
{
    if (_ppBarrierOption == NULL) throw gcnew NativeNullException ();
	return &**_ppBarrierOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BarrierOption>& Cephei::QL::Instruments::CBarrierOption::GetHandle ()
{
	if (_phBarrierOption == NULL)
	{
		_phBarrierOption = new Handle<QuantLib::BarrierOption> (*_ppBarrierOption);
	}
	return *_phBarrierOption;
}
#endif
bool Cephei::QL::Instruments::CBarrierOption::HasNative () 
{
	return (_ppBarrierOption != NULL);
}

Double Cephei::QL::Instruments::CBarrierOption::ImpliedVolatility (Double price, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations, Microsoft::FSharp::Core::FSharpOption<Double>^ minVol, Microsoft::FSharp::Core::FSharpOption<Double>^ maxVol)
{
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
        QuantLib::Real _price = (QuantLib::Real)ValueHelper::Convert (price);
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-4); //9a
        QuantLib::Size _maxEvaluations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxEvaluations) ? (QuantLib::Size)ValueHelper::Convert (maxEvaluations->Value) : 100); //9a
        QuantLib::Volatility _minVol = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (minVol) ? (QuantLib::Volatility)ValueHelper::Convert (minVol->Value) : 1.0e-7); //9a
        QuantLib::Volatility _maxVol = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (maxVol) ? (QuantLib::Volatility)ValueHelper::Convert (maxVol->Value) : 4.0); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppBarrierOption)->impliedVolatility ( _price,  _process,  _accuracy,  _maxEvaluations,  _minVol,  _maxVol );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IBarrierOption^ Cephei::QL::Instruments::CBarrierOption_Factory::Create (QL::Instruments::Barrier::TypeEnum barrierType, Double barrier, Double rebate, Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CBarrierOption ( barrierType,  barrier,  rebate,  payoff,  exercise,  QL_Pricer);
}
